﻿
//
// </copyright>

namespace System.Extensions.Data
{
  using System.Data;
  using System.Data.SqlClient;

  /// <summary>
  /// Extension methods for an <see cref="IDbConnection"/>.
  /// </summary>
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
  public static class IDbConnectionExtensions
  {
    /// <summary>
    /// Creates and returns a <see cref="IDbCommand"/> object associated with the connection.
    /// </summary>
    /// <param name="connection">The connection to associate the command to.</param>
    /// <param name="commandText">The text of the command.</param>
    /// <returns>An <see cref="IDbCommand"/> object associated with the connection.</returns>
    public static IDbCommand CreateCommand(this IDbConnection connection, string commandText)
    {
      return CreateCommand(connection, commandText, CommandType.Text);
    }

    /// <summary>
    /// Creates and returns a <see cref="IDbCommand"/> object associated with the connection.
    /// </summary>
    /// <param name="connection">The connection to associate the command to.</param>
    /// <param name="commandText">The text of the command.</param>
    /// <param name="commandType">The type of the command.</param>
    /// <returns>An <see cref="IDbCommand"/> object associated with the connection.</returns>
    public static IDbCommand CreateCommand(this IDbConnection connection, string commandText, CommandType commandType)
    {
      IDbCommand command = connection.CreateCommand();
      command.CommandText = commandText;
      command.CommandType = commandType;
      return command;
    }

    /// <summary>
    /// Creates an appropriate <see cref="IDbCommand"/> and executes a statement against the connection.
    /// </summary>
    /// <param name="connection">The connection to execute the command against.</param>
    /// <param name="commandText">The text of the command to execute.</param>
    /// <returns>The number of rows affected.</returns>
    public static int ExecuteCommandNonQuery(this IDbConnection connection, string commandText)
    {
      using (IDbCommand command = connection.CreateCommand(commandText))
      {
        return command.ExecuteNonQuery();
      }
    }

    /// <summary>
    /// Creates an appropriate <see cref="IDbCommand"/> and executes a statement against the connection.
    /// </summary>
    /// <param name="connection">The connection to execute the command against.</param>
    /// <param name="commandText">The text of the command to execute.</param>
    /// <param name="commandType">The type of the command to execute.</param>
    /// <returns>The number of rows affected.</returns>
    public static int ExecuteCommandNonQuery(this IDbConnection connection, string commandText, CommandType commandType)
    {
      using (IDbCommand command = connection.CreateCommand(commandText, commandType))
      {
        return command.ExecuteNonQuery();
      }
    }
  }
}
